package components.game
{
	import SEApp.se_util;
	
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	
	import classes.clt_events;
	import classes.clt_utils;
	
	import components.clt_arrowB;
	import components.clt_layer;
	import components.dialogs.clt_collageView;
	
	import flash.events.Event;

	public class clt_gameLayer extends clt_layer {
		private var _collageView: clt_collageView;
		private var _bests		: clt_gamePart;
		private var _lasts		: clt_gamePart;
		private var _vic		: clt_gamePart;
		private var _victorys	: clt_myVictory;
		
		public static const HEIGHT: Number = 290;
		
		public function clt_gameLayer(parent:uif_component, align:String=ALIGNTOP, lHeight: Number=130, vHeight: Number=160) {
			super(parent, HEIGHT, align);
			_bests = new clt_gamePart(this, 0, 0, width * 1/2, lHeight, '20 лучших коллажей за сутки');
			_lasts = new clt_gamePart(this, width * 1/2, 0, width * 1/2, lHeight, 'Последние коллажи');
			if (vHeight)
				_vic = new clt_gamePart(this, 0, _bests.height, width, vHeight, 'Победители в конкурсе', clt_vicItem);
			
			var b: clt_arrowB = new clt_arrowB(_bests, _bests.width - 113, 3, 110, 27, clt.classes.locale.GAMECAP, function(e: Event): void {
				clt.alert(clt.classes.locale.WARNING, clt.classes.locale.GAMEHELP);
			}, 'buttonB');
			b.textField.y = 3;
			
			updateList();

			workspace.addEventListener(clt_events.UPDATEGAME, doSendCollage);
			workspace.addEventListener(clt_events.INUSER, doInUser);
			workspace.addEventListener(clt_events.CHECKISGAME, doCheckIsGame);
		}

		protected function doInUser(e: Event): void {
			if (clt.user.myData.myVic.length > 0) {
				_victorys = new clt_myVictory(this, width * 2/3 - 10, -25, width * 1/3, 50);
				_victorys.assignList(clt.user.myData.myVic);
			}
			
			if (clt.user.myData.checkVictory.length) doNewVictory(clt.user.myData.checkVictory);
			
			if (clt.user.myData.victory.length && _vic)
				getGameUsers(clt.user.myData.victory, _vic);
			
			_collageView = new clt_collageView(clt.dialogLayer);
		}
		
		protected function doNewVictory(list: Array): void {
			clt.workspace.dispatchEvent(new ObjectEvent(clt_events.NEWVICTORY, clt.user.myData.checkVictory));
		}
		
		protected function doSendCollage(e: Event): void {
			updateList();
		}
		
		public function updateList(): void {
			getGameList('rate', _bests);
			getGameList('id;50', _lasts);
		}
		
		protected function getGameUsers(list: Array, line: clt_gamePart): void {
			if (list.length > 0) {
				clt.externalServer.getProfiles(se_util.getUids(list), function(a_users: Array): void {
					line.assignList(clt_utils.usersUnion(a_users, list));
				});
			} else line.assignList(list);			
		}
		
		protected function getGameList(order: String, line: clt_gamePart): void {
			clt.getGameList(clt.externalServer.viewer_id + ';' + order, line.assignList);
		}
		
		protected function doCheckIsGame(e: ObjectEvent): void {
			function checkSome(item: Object, i: int, arr: Array): Boolean {
				return (item && (item.uid == clt.user.uid));
			}
			e.object.result = (_lasts.list != null) && (_lasts.list.some(checkSome) || _bests.list.some(checkSome));
		}
	}
}